package com.kingbook.core.sql.dialect;

/**
 * @Author: Bucky
 * @Date: 2019/1/2
 * @Version 1.0
 */
public enum DBDialect {

    MYSQL {
        @Override
        public String getPaginationSQL(String sql, int offset, int limit) {
            sql = sql.endsWith(";") ? sql.substring(0, sql.length() - 1) : sql;
            return sql + " limit " + offset + "," + limit;
        }
    },
    ORACLE {
        @Override
        public String getPaginationSQL(String sql, int offset, int limit) {
            sql = sql.endsWith(";") ? sql.substring(0, sql.length() - 1) : sql;
            return "select t.*, rownum r from ( " + sql + " ) t where rownum between " + offset + " and " + (offset + limit);
        }
    };

    public String getPaginationSQL(String sql, int offset, int limit) {
        return "";
    }

    public static DBDialect getInstance(String productName) {
        productName = productName.toUpperCase();
        for (DBDialect dialect : DBDialect.values()) {
            if (productName.equals(dialect.name()))
                return dialect;
        }
        throw new NullPointerException("-----> 无法获取数据库名称." + productName);
    }
}
